#ifndef SOLARSYSTEMMODEL_H
#define SOLARSYSTEMMODEL_H

#include <QObject>
#include <QAbstractListModel>
#include <QTimer>
#include <QDateTime>
#include <QVector3D>

// 行星数据结构
struct Planet {
    QString name;
    QString chineseName;
    float radius;           // 行星半径（相对地球）
    float distance;         // 距离太阳的距离（天文单位）
    float orbitalPeriod;    // 公转周期（地球日）
    float rotationPeriod;   // 自转周期（地球日）
    QVector3D color;       // 行星颜色 RGB
    float currentAngle;     // 当前公转角度
    float rotationAngle;   // 当前自转角度
    bool isVisible;         // 是否可见
};

class SolarSystemModel : public QAbstractListModel
{
    Q_OBJECT

public:
    enum PlanetRoles {
        NameRole = Qt::UserRole + 1,
        ChineseNameRole,
        RadiusRole,
        DistanceRole,
        OrbitalPeriodRole,
        RotationPeriodRole,
        ColorRole,
        CurrentAngleRole,
        RotationAngleRole,
        IsVisibleRole
    };

    explicit SolarSystemModel(QObject *parent = nullptr);

    // QAbstractListModel 接口
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    QHash<int, QByteArray> roleNames() const override;

    // QML 暴露的方法
    Q_INVOKABLE void updatePlanetPositions();
    Q_INVOKABLE void togglePlanetVisibility(int index);
    Q_INVOKABLE void resetToCurrentTime();
    Q_INVOKABLE QVariantMap get(int index) const;

    // QML 暴露属性
    Q_PROPERTY(float timeSpeed READ timeSpeed WRITE setTimeSpeed NOTIFY timeSpeedChanged)
    Q_PROPERTY(bool isRunning READ isRunning WRITE setIsRunning NOTIFY isRunningChanged)

    float timeSpeed() const;
    void setTimeSpeed(float speed);
    bool isRunning() const;
    void setIsRunning(bool running);

signals:
    void timeSpeedChanged();
    void isRunningChanged();

private slots:
    void updateAnimation();

private:
    QList<Planet> m_planets;
    QTimer *m_animationTimer;
    float m_timeSpeed;
    bool m_isRunning;
    QDateTime m_startTime;

    void initializePlanets();
    void calculatePlanetPositions(const QDateTime &currentTime);
};

#endif // SOLARSYSTEMMODEL_H
